<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use app\Request;

/**
 * @ControllerAnnotation(title="用户")
 */
class User extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\User();
        
        $this->assign('getUserLevelList', $this->model->getUserLevelList());

        $this->assign('getStatusList', $this->model->getStatusList());





    }

    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $count = $this->model
                ->withJoin('userLevel', 'LEFT')
                ->where($where)
                ->count();
            $list = $this->model
                ->withJoin('userLevel', 'LEFT')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            foreach ($list as $k=>$v){
                if($v['superior'] >0){
                    $name = $this->model->where('id',$v['superior'])->find();
                    if($name){
                        $s_name = $name->title;
                    }else{
                        $s_name = '暂无上级';
                    }
                    $superior_name = $s_name;
                }else{
                    $superior_name = "暂无上级";
                }
                $list[$k]['superior_name'] = $superior_name;
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}